/*
 * WantToBuyDetailPresenter.java
 * business
 *
 * Created by chentao on 2017/5/12.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.wanttobuydetail;

import com.yidiandao.business.data.WantToBuyEntity;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.data.model.IWantToBuyModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/**
 * Presenter
 */
public class WantToBuyDetailPresenter implements WantToBuyDetailContract.Presenter {

    private WantToBuyDetailContract.View mDetailView;

    private IWantToBuyModel mWantToBuyModel;

    private String mWantId;

    private ShareEntity mShareEntity;

    public WantToBuyDetailPresenter(WantToBuyDetailContract.View view, IWantToBuyModel model, String wantId) {
        mDetailView = view;
        mWantToBuyModel = model;
        mWantId = wantId;

        mDetailView.setPresenter(this);
    }

    @Override
    public void start() {
        getWantToBuyDetail();
    }

    private void getWantToBuyDetail() {
        mWantToBuyModel.getWantToBuyDetail(mWantId, mDetailView.getPagerTag(), new IWantToBuyModel.OnWantToBuyDetailListener() {
            @Override
            public void onHeader(WantToBuyEntity entity) {
                mDetailView.refreshContent(SuperLoadingLayout.STATE_SUCCESS);
                mDetailView.refreshHeader(entity);

                // 平台包含的与用户求购的商品类型一致的商品列表

                mShareEntity = entity.getShareEntity();

                getOfferList(true, entity.getGoodsList());
            }

            @Override
            public void onFailed() {
                mDetailView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void getOfferList(final boolean isRefreshing, List<Goods> similarGoodsList) {
        mWantToBuyModel.getWantToBuyOfferList(isRefreshing, similarGoodsList, mWantId, mDetailView.getPagerTag(), new IWantToBuyModel.OnOfferListListener() {
            @Override
            public void onSuccess(List<DisplayListItem> listItems, boolean emptyData, boolean hasMoreData) {
                mDetailView.setDisplayItems(listItems);
                mDetailView.loadMoreFinish(emptyData, hasMoreData);
            }

            @Override
            public void onFailed() {
                mDetailView.loadMoreError();
            }
        });
    }

    @Override
    public void submitOffer(String price, String shippingFee, String phone) {
        mWantToBuyModel.submitOffer(mDetailView.getPagerTag(), mWantId, price, shippingFee, phone, new IWantToBuyModel.OnNetListener<Object>() {
            @Override
            public void onSuccess(Object obj) {
                mDetailView.showSuccessDialog();
            }

            @Override
            public void onFailed(String message) {
                mDetailView.showFailedDialog(message);
            }
        });
    }

    @Override
    public void doShare() {
        mDetailView.showShareDialog(mShareEntity);
    }

}
